ํ๋ ํ๋ก ํธ์๋ ์ํ ๊ด๋ฆฌ: Zustand์ Pinia๋ก ๊ฐ๋ณ๊ฒ ์์ํ๊ธฐ
์ปดํฌ๋ํธ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ๋ค ๋ณด๋ฉด 'Prop Drilling'(์ฌ๋ฌ ๋จ๊ณ๋ฅผ ๊ฑฐ์ณ Props๋ฅผ ์ ๋ฌ) ๋ฌธ์ ์ ์ง๋ฉดํ๊ฒ ๋ฉ๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด Redux ๊ฐ์ ๊ฑฐ๋ํ ๋๊ตฌ๋ฅผ ์ธ ์๋ ์์ง๋ง, ์ต๊ทผ์๋ ๋ ๋จ์ํ๊ณ ์ง๊ด์ ์ธ Zustand์ Pinia๊ฐ ๋์ธ๋ก ์๋ฆฌ ์ก์์ต๋๋ค.
1. ์ํ ๊ด๋ฆฌ๋ ๋ฌด์์ธ๊ฐ?
์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ๋ฌ ์ปดํฌ๋ํธ๊ฐ ํจ๊ป ์ฌ์ฉํ๋ ๊ณต์ ๋ฐ์ดํฐ(์ฌ์ฉ์ ์ ๋ณด, ํ ๋ง, ์ฅ๋ฐ๊ตฌ๋ ๋ฑ)๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ ๋งํฉ๋๋ค.
2. Zustand: React๋ฅผ ์ํ ์๊ณ ๊ฐ๋ ฅํ ๋๊ตฌ
Zustand๋ ๋ ์ผ์ด๋ก '์ํ'๋ผ๋ ๋ป์ผ๋ก, ์ค์ ์ด ๊ฑฐ์ ์๊ณ ๋งค์ฐ ๊ฐ๋ฒผ์ด ๋ฆฌ์กํธ์ฉ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
Zustand ์ฌ์ฉ๋ฒ (์์)
import { create } from 'zustand';
// ์คํ ์ด ์์ฑ
const useStore = create((set) => ({
count: 0,
increase: () => set((state) => ({ count: state.count + 1 })),
removeAll: () => set({ count: 0 }),
}));
// ์ปดํฌ๋ํธ์์ ์ฌ์ฉ
function Counter() {
const { count, increase } = useStore();
return (
<div>
<h1>{count}</h1>
<button onClick={increase}>์ฆ๊ฐ</button>
</div>
);
}3. Pinia: Vue์ ๊ณต์ ์ํ ๊ด๋ฆฌ ๋๊ตฌ
Vue 3๋ถํฐ๋ ๊ธฐ์กด์ Vuex ๋์ Pinia๋ฅผ ๊ณต์์ ์ผ๋ก ๊ถ์ฅํฉ๋๋ค. ํ์ ์คํฌ๋ฆฝํธ ์ง์์ด ์๋ฒฝํ๊ณ ๊ตฌ์กฐ๊ฐ ๋จ์ํฉ๋๋ค.
Pinia ์ฌ์ฉ๋ฒ (์์)
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++;
},
},
});4. ์ Redux๊ฐ ์๋ ์ด๋ค์ ์ ํํ ๊น?
- ๋ฌ๋ ์ปค๋ธ: Redux๋ ๋ณด์ผ๋ฌํ๋ ์ดํธ ์ฝ๋๊ฐ ๋ง๊ณ ๋ณต์กํ์ง๋ง, Zustand/Pinia๋ ๋ช ๋ถ ๋ง์ ๋ฐฐ์์ ๋ฐ๋ก ์ธ ์ ์์ต๋๋ค.
- ๋ฒ๋ค ํฌ๊ธฐ: ๋งค์ฐ ๊ฐ๋ฒผ์ ์ฑ๋ฅ ์ต์ ํ์ ์ ๋ฆฌํฉ๋๋ค.
- ์ ์ฐ์ฑ: ํน์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ ํ์ง ์์ ๊ฐ๋ฐ์ ์ทจํฅ์ ๋ง๊ฒ ์ค๊ณํ ์ ์์ต๋๋ค.
5. ๊ฒฐ๋ก
ํ๋ก์ ํธ๊ฐ ์์ฒญ๋๊ฒ ํฌ๊ณ ์๊ฒฉํ ๊ท์น์ด ํ์ํ๋ค๋ฉด Redux๊ฐ ์ฌ์ ํ ์ ํจํ ์ ์์ต๋๋ค. ํ์ง๋ง ๋๋ถ๋ถ์ ํ๋์ ์น ์๋น์ค์ ์คํํธ์ ํ๋ก์ ํธ์์๋ **Zustand(React)**๋ **Pinia(Vue)**๋ฅผ ์ ํํ๋ ๊ฒ์ด ์์ฐ์ฑ ์ธก๋ฉด์์ ํจ์ฌ ํ์ํ ์ ํ์ ๋๋ค.